import { requestGet,requestPost } from '../../api/request'
Page({
  data: {
    formData:{
      "id":'',
      'mobile':'',
      // 'password':'',
      "hphms":[],
      "openid":'',
      "startTime":'',
      "endTime":'',
      "count":'',
      "yxqs":'',
      "msg":'',
      "applyStatus":'',
      "remark":'',
      "status":'',
      "expCount":'',
    },
    txt:"",
    showFlag:true,
    show:1,
    carinfo1:{},
    carinfo2:{},
    carinfo3:{},
    rules:[
      {
        name: 'mobile',
        rules: [
          {required: true, errorMessage: '手机号不能为空'},
          {mobile: true, message: '电话格式不对'}
        ]
      },
      // {
      //   name: 'password',
      //   rules: [
      //     {required: true, errorMessage: '密码不能为空'},
      //   ]
      // }
     
    ]
  },
  onLoad: function () {
      this.userInfo = wx.getStorageSync("userInfo");
      // 获取用户详情
      requestGet("/wx/user/info?openid="+this.userInfo.openid).then(res=>{
        if(res.code == 200){
          if(res.data.openid != null){
            let hphmsData = res.data.hphms[0];
            this.setData({
              showFlag:true,
              formData:{
                mobile:res.data.mobile,
                // password:res.data.password,
                openid:res.data.openid,
                hphms:res.data.hphms,
                authorization:res.data.authorization,
                startTime:hphmsData != undefined? hphmsData.startTime :"",
                endTime:hphmsData != undefined? hphmsData.endTime :"",
                count:hphmsData != undefined? hphmsData.count :"",
                yxqs:hphmsData != undefined? hphmsData.yxqs :"",
                applyStatus:hphmsData != undefined? hphmsData.applyStatus :"",
                msg:hphmsData != undefined? hphmsData.msg :"",
                car:hphmsData != undefined? hphmsData.msg :"",
                id:hphmsData != undefined? hphmsData.id :"",
                status:hphmsData != undefined? hphmsData.status :"",
                expCount:hphmsData != undefined? hphmsData.expCount :"",
                remark:res.data.remark,
              },
               carinfo1:{carNum:res.data.hphms[0] != undefined? res.data.hphms[0].hphm:""},
               carinfo2:{carNum:res.data.hphms[1] != undefined? res.data.hphms[1].hphm:""},
               carinfo3:{carNum:res.data.hphms[2] != undefined? res.data.hphms[2].hphm:""},
            }) 
          }  
        }
      })
      requestGet("/wx/user/txt").then(res=>{
        this.setData({
          txt: res.data.replaceAll("\\n","\n")
        })
      })
      requestGet("/wx/user/show").then(res=>{
        this.setData({
          show: res.data
        })
      })
  },

  handInputChange (e) {
    this.setData({
        [`formData.${e.target.dataset.field}`]: e.detail.value
    })
  },

  //保存
  submitForm (){
    const form = this.selectComponent('#form');
    form.validate((valid, errors) => {
      if (valid) {
        // 所有验证通过
        let data = this.data.formData;
        console.log('表单数据真实性验证通过', data);
        requestPost('/wx/user/save',data).then(res => {
          if(res.code == 200){
            wx.showToast({
              title: '保存成功',
              icon:'none'
            })
            this.onLoad()
          }
        })
      } else {
        const firstError = Object.keys(errors)
        if(firstError.length) {
            wx.showToast({
                title: errors[firstError[0]].message,
                icon: 'none'
            })
        }
      }
    });
  },
  getCarNum1 (e){
    let carinfo = {}
    carinfo.carNum =e.detail.carNum
    carinfo.inputPlates =e.detail.inputPlates
    carinfo.isPlate = e.detail.isPlate
    carinfo.isNewEnergy = e.detail.isNewEnergy
    this.setData({
      carinfo1:carinfo,
      ['formData.hphms[0].hphm']:e.detail.carNum
    })
    // console.log(this.formData);
    //this.data.formData.hphms[0] = e.detail.carNum
  },
  getCarNum2 (e){
    let carinfo = {}
    carinfo.carNum =e.detail.carNum
    carinfo.inputPlates =e.detail.inputPlates
    carinfo.isPlate = e.detail.isPlate
    carinfo.isNewEnergy = e.detail.isNewEnergy
    this.setData({
      carinfo2:carinfo,
      ['formData.hphms[1].hphm']:e.detail.carNum
    })
    // this.data.formData.hphms[1] = e.detail.carNum
  },
  getCarNum3 (e){
    let carinfo = {}
    carinfo.carNum =e.detail.carNum
    carinfo.inputPlates =e.detail.inputPlates
    carinfo.isPlate = e.detail.isPlate
    carinfo.isNewEnergy = e.detail.isNewEnergy
    this.setData({
      carinfo3:carinfo,
      ['formData.hphms[2].hphm']:e.detail.carNum
    })
    // this.data.formData.hphms[2] = e.detail.carNum
  }, 
  doApply(){
    requestGet('/JjzApplylog/doApply?id='+this.data.formData.id).then(res => {
      if(res.code == 200){
        wx.showToast({
          title: '申请成功',
          icon:'none'
        })
      }
    })
  }
})
